package org.ptkparty.client.activity;

import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.ptkparty.client.ClientFactory;
import org.ptkparty.client.Context;
import org.ptkparty.client.model.ChampionshipProxy;
import org.ptkparty.client.model.UserProxy;
import org.ptkparty.client.place.ResultsPlace;
import org.ptkparty.client.request.ChampionshipRequest;
import org.ptkparty.client.view.championships.ChampionshipView;
import org.ptkparty.client.view.championships.ChampionshipsView;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class ChampionshipsActivity implements Activity {

	private static Driver driver = GWT.create(Driver.class);

	interface Driver extends RequestFactoryEditorDriver<List<ChampionshipProxy>, ListEditor<ChampionshipProxy, ChampionshipView>> {
	}

	private ClientFactory clientFactory;
	private ChampionshipRequest championchipRequest;
	private ChampionshipsView view;
	private List<UserProxy> userList;

	public ChampionshipsActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view = clientFactory.getChampionchipView();
		view.setPresenter(this);
		panel.setWidget(view);

		clientFactory.getFactory().userRequest().getAllUser().fire(new Receiver<List<UserProxy>>() {

			@Override
			public void onSuccess(final List<UserProxy> response) {

				userList = response;

				clientFactory.getFactory().championchipRequest().getAllForAdmin().fire(new Receiver<List<ChampionshipProxy>>() {

					@Override
					public void onSuccess(List<ChampionshipProxy> response) {
						championchipRequest = clientFactory.getFactory().championchipRequest();

						driver.initialize(clientFactory.getEventBus(), clientFactory.getFactory(), view.asEditor());

						driver.edit(response, championchipRequest);

						view.initProvider();

						view.selectFirstChampionship();

						championchipRequest.save(view.asEditor().getList()).to(new Receiver<Void>() {

							@Override
							public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
								driver.setConstraintViolations(violations);
							}

							@Override
							public void onSuccess(Void response) {

							}

						});
					}
				});
			}
		});
	}

	@Override
	public String mayStop() {
		return null;
	}

	@Override
	public void onCancel() {
	}

	@Override
	public void onStop() {
	}

	public ChampionshipProxy addChampionship() {
		ChampionshipProxy newChampionship = championchipRequest.create(ChampionshipProxy.class);
		newChampionship.setName("Nouveau championnat");
		return newChampionship;
	}

	public void delete(final ChampionshipProxy selectedProxy) {
		if (selectedProxy != null) {
			clientFactory.getFactory().championchipRequest().remove().using(selectedProxy).fire(new Receiver<Void>() {

				@Override
				public void onSuccess(Void response) {
					view.removeChampionshipEditor(selectedProxy);
				}

			});
		}
	}

	public void save() {
		driver.flush().fire();
	}

	public List<UserProxy> getUserList() {
		return userList;
	}

	public void regenerate() {
		// final DialogBox dialogBox = new DialogBox(false, true);
		// dialogBox.setText("Confirmer");
		// dialogBox.add(new HTML(""));
		// dialogBox.add(new Button("Ok", new ClickHandler() {
		//
		// @Override
		// public void onClick(ClickEvent event) {
		// dialogBox.hide();
		clientFactory.getPtkService().regenerateResults(Context.get().getCurrentChampionship(), new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(Boolean result) {
				clientFactory.getPlaceController().goTo(new ResultsPlace(Context.get().getCurrentChampionship()));
			}
		});
		// }
		// }));
		// dialogBox.add(new Button("Annuler", new ClickHandler() {
		//
		// @Override
		// public void onClick(ClickEvent event) {
		// dialogBox.hide();
		// }
		// }));
	}
}
